package cn.itsource.pethome.org.service.impl;

import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.org.constant.Constant;
import cn.itsource.pethome.org.domain.Product;
import cn.itsource.pethome.org.domain.ProductDetail;
import cn.itsource.pethome.org.mapper.ProductMapper;
import cn.itsource.pethome.org.query.ProductQuery;
import cn.itsource.pethome.org.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description: 描述:
 * @date: 2020/8/9 14:29
 * @author: summer
 * version: 1.0
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 功能描述: 加载列表 查询所有满足条件的列表
     * @param productQuery
     * @return: cn.itsource.pethome.basic.util.PageList<cn.itsource.pethome.org.domain.Product>
     * @Date: 2020/8/20
     */
    @Override
    public PageList<Product> findAll(ProductQuery productQuery) {
        Long total = productMapper.findCountAll(productQuery);
        if(total.equals(0L)){
            return new PageList();
        }
        List<Product> list = productMapper.findAll(productQuery);
            return new PageList<Product>(total, list);
    }

    /**
     * 功能描述: 根据id删除列表
     * @param id
     * @return: void
     * @Date: 2020/8/20
     */
    @Override
    @Transactional
    public void delProduct(Long id) {
        productMapper.delProduct(id);
        productMapper.delProductDetail(id);
    }

    /**
     * 功能描述: 根据id批量删除列表
     * @param id
     * @return: void
     * @Date: 2020/8/20
     */
    @Override
    @Transactional
    public void batchDelProduct(Long[] id) {
        productMapper.batchDelProduct(id);
        productMapper.batchDelProductDetail(id);
    }

    /**
     * 功能描述: 添加列表
     * @param product
     * @return: void
     * @Date: 2020/8/20
     */
    @Override
    @Transactional
    public void save(Product product) {
        productMapper.save(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        productMapper.saveDetal(productDetail);
    }

    /**
     * 功能描述: 修改列表
     * @param product
     * @return: void
     * @Date: 2020/8/20
     */
    @Override
    @Transactional
    public void update(Product product){
        productMapper.update(product);
        ProductDetail detail = product.getProductDetail();
        detail.setId(product.getProductDetail().getId());
        //System.out.println("测试"+detail);
        productMapper.updateDetal(detail);
    }

    //上架
    @Override
    @Transactional
    public void productAdd(Long[] id) {
        for (Long aLong : id) {
            Product product = productMapper.loadById(aLong);
            product.setState(Constant.ONSHELVES);
            product.setOnsaletime(new Date()); //上架时间
            productMapper.update(product);
        }
    }

    //下架
    @Override
    public void productDown(Long[] id) {
        for (Long aLong : id) {
            Product product = productMapper.loadById(aLong);
            product.setState(Constant.OFFSHELVES);
            product.setOffsaletime(new Date());
            productMapper.update(product);
        }
    }

    @Override
    public Product particulars(Long id) {
        return productMapper.findById(id);
    }
}
